package com.zkingsoft.actions.admin;

import java.util.Arrays;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.config.Dictionary;
import com.zkingsoft.model.Nav;
import com.zkingsoft.model.ShowElementCate;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.NavService;
import com.zkingsoft.services.ShowElementCateService;

/**
 * @project MatrixCms
 * @package NavController
 * @class com.zkingsoft.actions.admin.NavController
 * @author Matrix-J E-mail:<935090232@qq.com>
 * @date 2016年5月5日10:31
 * @description 导航栏管理
 */
@Controller
@RequestMapping(value = "/admin/nav")
public class NavController extends BaseController {

	private static final Logger log = Logger.getLogger(NavController.class);

	@Resource
	private NavService currentService;

	@Resource
	private ShowElementCateService showElementCateService;

	@RequestMapping(value = "/all")
	public @ResponseBody AjaxResult all(HttpServletRequest request, Nav nav) {
		nav.setCompanyId(getMe(request).getCompanyId());
		return findByModel(currentService, nav);
	}

	/**
	 * findByModel
	 */
	@RequestMapping(value = "/findByModel")
	public @ResponseBody AjaxResult findByModel(Nav navQuery, HttpServletRequest request) {
		navQuery.setCompanyId(getMe(request).getCompanyId());
		return new AjaxResult(AjaxResult.STATUS_OK, null, "执行成功", currentService.findByModel(navQuery), 0, 0);
	}

	@RequestMapping(value = "/findById")
	public @ResponseBody AjaxResult findById(Long id) {
		Nav nav = super.findById(currentService, id);
		return new AjaxResult(AjaxResult.STATUS_OK, null, null, Arrays.asList(nav), 0, 0);
	}

	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(Nav navQuery, HttpServletRequest request) {
		if (navQuery.getId() != null) {
			return modify(currentService, navQuery, "导航栏");
		} else {
			navQuery.setCompanyId(getMe(request).getCompanyId());
			return add(currentService, navQuery, "导航栏");
		}
	}

	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		return remove(currentService, keys);
	}

	@RequestMapping(value = "/editForm")
	public String editForm(Long id, int plat, HttpServletRequest request) {
		Nav nav;
		// 判断导航栏类型进行跳转
		String page = plat == Dictionary.NAV_PLAT_PC ? "admin/nav-pcform" : "admin/nav-movbileform";
		if (id != null) {
			nav = currentService.findById(id);
			request.setAttribute("obj", nav);
		}
		log.info(page);
		return page;
	}

	@RequestMapping(value = "/getCate")
	public @ResponseBody AjaxResult all(HttpServletRequest request, ShowElementCate showElementCateQuery) {
		showElementCateQuery.setCompanyId(getMe(request).getCompanyId());
		return new AjaxResult(AjaxResult.STATUS_OK, null, null,
				showElementCateService.findByModel(showElementCateQuery), 0, 0);
	}

}